package day10;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Demo09 {
    public static void main(String[] args) {
        /*
         *  学号 姓名 性别 年龄 联系电话
            11, 张三, 男, 25, 18789009989
            12, 杨丽, 女, 23, 15678776678
            13, 小明, 男, 30, 13345433451
            14, 小月, 男, 19, 18789775567
         */
        String filename = "student.txt";
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        new BufferedInputStream(
                                new FileInputStream(filename)
                        ), StandardCharsets.UTF_8))){
            String row = in.readLine();
            System.out.println(row);
            List<Student> students = new ArrayList<>();
            while ((row = in.readLine())!=null){
                System.out.println(row);
                Student student = Student.parse(row);
                students.add(student);
            }

            System.out.println("输出所有男人:");
            students.stream().filter(student -> student.getGender().equals("男"))
                .forEach(System.out::println);

            System.out.println("按照年龄排序输出:");
            //students.sort(Comparator.comparingInt(Student::getAge).reversed());
            //students.forEach(System.out::println);
            students.stream().sorted(Comparator.comparingInt(Student::getAge))
                    .forEach(System.out::println);

            //写到文件中
            PrintWriter out = new PrintWriter("result.txt");
            //students.forEach(out::println);
            students.stream().sorted(Comparator.comparingInt(Student::getAge))
                    .forEach(out::println);
            out.close();

        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
class Student{
    private int id;
    private String name;
    private String gender;
    private int age;
    private String phone;

    public Student() {
    }

    public static Student parse(String row){
        //14, 小月, 男, 19, 18789775567
        String[] data = row.split(",\\s*");
        if (data.length != 5){
            throw new IllegalArgumentException("文件格式错误!");
        }
        int id = Integer.parseInt(data[0]);
        String name = data[1];
        String gender = data[2];
        int age = Integer.parseInt(data[3]);
        String phone = data[4];
        return new Student(id,name, gender, age, phone);
    }

    public Student(int id, String name, String gender, int age, String phone) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.phone = phone;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return id + ", " + name + ", " + gender + ", " + age + ", " + phone;
    }
}
